﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard;
using Orchard.Caching;
using Orchard.Environment.Extensions;
using EMS.HTMLKickStart.Models;
using System.Web;

namespace EMS.HTMLKickStart.Services {
    
    public class HTMLKickStartService : IHTMLKickStartService {
        private readonly IWorkContextAccessor _wca;
        private readonly ICacheManager _cacheManager;
        private readonly ISignals _signals;
        
        public HTMLKickStartService(IWorkContextAccessor wca, ICacheManager cacheManager, ISignals signals) {
            _wca = wca;
            _cacheManager = cacheManager;
            _signals = signals;
        }

        public bool GetIsIncludeHTMLKickStartEveryPage() {
            return bool.Parse(
                _cacheManager.Get(
                    "EMS.HTMLKickStart.IsIncludeHTMLKickStartEveryPage",
                    ctx => {
                        ctx.Monitor(_signals.When("EMS.HTMLKickStart.Changed"));
                        WorkContext workContext = _wca.GetContext();
                        var htmlKickStartSettings =
                            (HTMLKickStartSettingsPart)workContext
                                .CurrentSite
                                .ContentItem
                                .Get(typeof(HTMLKickStartSettingsPart));
                        return htmlKickStartSettings.IsIncludeHTMLKickStartEveryPage.ToString();
                    })
                );
        }
    }
}